#ifndef __SOC_UART_H__
#define __SOC_UART_H__

#include "iperipheral.h"
#include "soc/soc_comm_def.h"
#include "interface/if_soc_uart.h"

#ifdef HAL_UART_MODULE_ENABLED

//DMA接收BUFFER大小
#define SOC_UART_DMA_RECV_BUFFER_SIZE 64

/// @brief soc 串口
class SOC_Uart : public IF_SOC_Uart
{
public:
    explicit SOC_Uart(UartDevId id = UartDevId::None, void *handle = nullptr);
    virtual ~SOC_Uart();

public:
    void WriteByte(uint8_t value);
    void Write(const uint8_t *buf, int len);
    void Write(const char *str);
    void Write(const std::string &str);
    void WriteLine(const char *str);
    void WriteLine(const std::string &str);

    //DMA
    void WriteDMA(const uint8_t *buf, int len);
    bool IsDMAIdle();
    
    int ReadByte();

    void StartReceive_IT();
    void StartReceive_DMA(); 

public:
    void SetDataReceivedCallBack(const IF_SOC_Uart::DATA_RECEIVED_CB &func);
    void SetDataReceivedCallBack_DMA(const IF_SOC_Uart::DATA_RECEIVED_CB_DMA &func);
    void RaiseDataReceivedCallBack();
    void RaiseIdleEventCallBack(); //FOR DMA

private:
    IF_SOC_Uart::DATA_RECEIVED_CB m_dataRecvCb = nullptr;
    IF_SOC_Uart::DATA_RECEIVED_CB_DMA m_dataRecvCb_DMA = nullptr;

    uint8_t m_recvBuffer = 0; //一般中断接收
    
    uint8_t m_recvBuffer_DMA1[SOC_UART_DMA_RECV_BUFFER_SIZE];
    uint8_t m_recvBuffer_DMA2[SOC_UART_DMA_RECV_BUFFER_SIZE];
    int m_currentDMABufferIndex = -1;
};

#endif 

#endif // __SOC_UART_H__
